﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;

namespace Libs.HR
{
    public class Contracts
    {
        public int ContractID { get; set; }
        public int EmployeeID { get; set; }
        public int TypeID { get; set; }
        public int CompanyID { get; set; }
        public string ContractNumber { get; set; }
        public DateTime SignedTime { get; set; }
        public DateTime BeginTime { get; set; }
        public DateTime EndTime { get; set; }
        public string Description { get; set; }
        public DateTime LogTime { get; set; }
        public bool IsFinish { get; set; }
        public int ReturnValue { get; set; }

        public Contracts()
        {

        }

        ~Contracts()
        {

        }

        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Lấy giá trị Contracts theo ID
        /// </summary>
        /// <param name="contractID"></param>
        /// <returns></returns>
        public Contracts Get(int contractID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Contracts>("sp_Contracts_Select",
                new SqlParameter("@ContractID", contractID));
        }

        /// <summary>
        /// Lấy giá trị Contracts theo ID
        /// </summary>
        /// <returns></returns>
        public Contracts Get()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Contracts>("sp_Contracts_Select",
                new SqlParameter("@ContractID", ContractID));
        }

        /// <summary>
        /// Thêm mới Contracts
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[10];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@EmployeeID", EmployeeID);
            pars[2] = new SqlParameter("@TypeID", TypeID);
            pars[3] = new SqlParameter("@CompanyID", CompanyID);
            pars[4] = new SqlParameter("@ContractNumber", ContractNumber);
            pars[5] = new SqlParameter("@SignedTime", SignedTime);
            pars[6] = new SqlParameter("@BeginTime", BeginTime);
            pars[7] = EndTime == DateTime.MaxValue ? new SqlParameter("@EndTime", DBNull.Value) : new SqlParameter("@EndTime", EndTime);
            pars[8] = new SqlParameter("@Description", Description);
            pars[9] = new SqlParameter("@IsFinish", IsFinish);

            db.ExecuteNonQuerySP("sp_Contracts_Insert", pars);
            ContractID = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Cập nhật Contract
        /// </summary>
        public void Update()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[11];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@ContractID", ContractID);
            pars[2] = new SqlParameter("@EmployeeID", EmployeeID);
            pars[3] = new SqlParameter("@TypeID", TypeID);
            pars[4] = new SqlParameter("@CompanyID", CompanyID);
            pars[5] = new SqlParameter("@ContractNumber", ContractNumber);
            pars[6] = new SqlParameter("@SignedTime", SignedTime);
            pars[7] = new SqlParameter("@BeginTime", BeginTime);
            pars[8] = EndTime == DateTime.MaxValue ? new SqlParameter("@EndTime", DBNull.Value) : new SqlParameter("@EndTime", EndTime);
            pars[9] = new SqlParameter("@Description", Description);
            pars[10] = new SqlParameter("@IsFinish", IsFinish);

            db.ExecuteNonQuerySP("sp_Contracts_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        /// <param name="contractID"></param>
        public void Delete(int contractID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            
            db.ExecuteNonQuerySP("sp_Contracts_Delete",
                new SqlParameter("@ContractID", contractID));
        }

        /// <summary>
        /// Xoá Contract
        /// </summary>
        public void Delete()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_Contracts_Delete",
                new SqlParameter("@ContractID", ContractID));
        }

        public DataTable GetTList(int employeeID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetDataTableSP("sp_Contracts_SelectList",
                employeeID == 0 ? new SqlParameter("@EmployeeID", DBNull.Value) : new SqlParameter("@EmployeeID", employeeID));
        }

    }
}
